package ee.wm.ilmataat;

import ee.wm.ilmataat.xml.WeatherXmlImporter;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.Context;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.RemoteViews;

public class WidgetProvider extends AppWidgetProvider implements LocationListener {
	
	protected LocationManager locationManager;
	protected Station currentStation;
	protected static int renderCount = 0;
	private WeatherXmlImporter importer = new WeatherXmlImporter();
	
	@Override
	public void onLocationChanged(Location location) {
		if (location != null) {
			Log.d("Ilmataat", "LOCATION CHANGED LAT: " + location.getLatitude());
			Log.d("Ilmataat", "LOCATION CHANGED LNG: " + location.getLongitude());

//			currentStation = LocationComparison.findClosestStation(
//				location,
//				importer.importStations(this.g)
//			);
			
			Log.d("Ilmataat", "FOUND: " + currentStation.getName());
		}
		
		
	}
	
	@Override
	public void onProviderDisabled(String provider) {
		// TODO Auto-generated method stub
	}

	@Override
	public void onProviderEnabled(String provider) {
		// TODO Auto-generated method stub
	}
	
	@Override
	public void onStatusChanged(String provider, int status, Bundle extras) {
		// TODO Auto-generated method stub
	}
	
	public void onEnabled(Context context) {
		
    }
	
	public void onDisabled(Context context) {
		locationManager.removeUpdates(this);
	}
	
	@Override
    public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds) {
		LocationManager locationManager = (LocationManager)context.getSystemService(Context.LOCATION_SERVICE);
		locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 1000, 10, this);
		
		Location location = locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
		
		if (location == null) {
			locationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		}
		
		if (location != null) {
			Log.d("Ilmataat", "onUpdate location: " + location.toString());
			
			onLocationChanged(location);
		} else {
			Log.d("Ilmataat", "onUpdate location still not available");
		}
		
		for (int i=0; i < appWidgetIds.length; i++) {
            updateWidget(context, appWidgetManager, appWidgetIds[i], currentStation);
        }
    }
	
	static void updateWidget(Context context, AppWidgetManager appWidgetManager, int appWidgetId, Station station) {
		CharSequence text = context.getString(R.string.searching_location);
        
        if (station != null) {
        	text = station.getName() + ": " + station.getAirTemperature();
        }

        // Construct the RemoteViews object.  It takes the package name (in our case, it's our
        // package, but it needs this because on the other side it's the widget host inflating
        // the layout from our package).
        RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget);
        views.setTextViewText(R.id.widget_text, text);

        // Tell the widget manager
        appWidgetManager.updateAppWidget(appWidgetId, views);
    }
}
